﻿# -*- coding:utf-8 -*-
import time
import smtplib
import os
from selenium import webdriver
from email.mime.text import MIMEText
from email.header import Header
from selenium.webdriver.common.keys import Keys

# ========================= 信息-需修改 =========================
username = "#######"  # 账号
pwd = "############"  # 密码
zy = "#############"  # 专业
sjh = "#############" # 手机号
qsh = "############"  # 寝室号
yjsms = "###########" # 研究生秘书
dzse = "上海" # 地址省
dzsi = "上海市" # 地址市
dzq = "嘉定区" # 地址区
dzxx = "西上海御庭159-1901" # 地址详细
loading_time = 3  # 预留页面加载时间10秒 , 视电脑配置修改

# ========================= url-无需修改 =========================
login_url = "https://ehall.jlu.edu.cn/jlu_portal/login"  # 登录url
submit_url = "https://ehall.jlu.edu.cn/infoplus/form/YJSMRDK/start"  # 研究生每日打卡url
remind_url = "https://ehall.jlu.edu.cn/taskcenter/workflow/done"  # 已完成事项url

# ========================= xpath-无需修改 =========================
uid_xpath = './/input[@name="username"]'  # 账号xpath
pwd_xpath = './/input[@name="password"]'  # 密码xpath
button_xpath = './/input[@name="login_submit"]'  # 登录按钮xpath
zy_xpath = './/input[@id="V1_CTRL40"]' # 专业
nj_xpath = './/select[@id="V1_CTRL41"]' # 年级
nj2018_xpath = './/select[@id="V1_CTRL41"]/option[10]' # 2018
nj2020_xpath = './/select[@id="V1_CTRL41"]/option[12]' # 2020 
xq_xpath = './/select[@id="V1_CTRL42"]' # 校区
zxxq_xpath = './/select[@id="V1_CTRL42"]/option[2]' # 中心校区
sj_xpath= './/input[@id="V1_CTRL67"]' # 手机号
yjsms_xpath = '/html/body/div[4]/form/div/div[3]/div[3]/div[1]/div[1]/div[2]/table/tbody/tr[2]/td/div/table/tbody/tr[5]/td[4]/font/div/div/div/div/input' # 研究生秘书
xw_xpath = './/input[@id="V1_CTRL64"]' # 校外
dzse_xpath = '/html/body/div[4]/form/div/div[3]/div[3]/div[1]/div[1]/div[2]/table/tbody/tr[2]/td/div/table/tbody/tr[11]/td[2]/div/div/div/div/input' # 地址省
dzsi_xpath = '/html/body/div[4]/form/div/div[3]/div[3]/div[1]/div[1]/div[2]/table/tbody/tr[2]/td/div/table/tbody/tr[11]/td[4]/div/div/div/div/input' # 地址市
dzq_xpath = '/html/body/div[4]/form/div/div[3]/div[3]/div[1]/div[1]/div[2]/table/tbody/tr[2]/td/div/table/tbody/tr[11]/td[6]/div/div/div/div/input' # 地址区
dzxx_xpath = './/input[@id="V1_CTRL39"]' # 地址详细
gfxf_xpath = '/html/body/div[4]/form/div/div[3]/div[3]/div[1]/div[1]/div[2]/table/tbody/tr[2]/td/div/table/tbody/tr[12]/td[2]/div/font/div/font[1]/input' # 高风险否
gylmc_xpath = './/select[@id="V1_CTRL7"]' # 公寓楼名称
nybgy_xpath = './/select[@id="V1_CTRL7"]/option[13]' # 南苑8公寓
nylgy_xpath = './/select[@id="V1_CTRL7"]/option[11]' # 南苑6公寓
qsh_xpath = './/input[@id="V1_CTRL8"]' # 寝室号
ss_xpath = './/input[@id="V1_CTRL44"]' # 硕士
bs_xpath = './/input[@id="V1_CTRL45"]' # 博士
q1_xpath = './/input[@id="V1_CTRL28"]' # 7-8
q2_xpath = './/input[@id="V1_CTRL19"]' # 11-12
q3_xpath = './/input[@id="V1_CTRL23"]' # 17-18
confirm_xpath = '/html/body/div[4]/form/div/div[3]/div[3]/div[2]/ul/li/a'  # 确认按钮xpath
ok_xpath = './/button[@class="dialog_button default fr"]'  # 好按钮xpath

# ========================= 驱动-无需修改 =========================
os.system('taskkill /IM chrome.exe')  # 关闭chrome
os.system('taskkill /IM chrome.exe')  # 关闭chrome
os.system('taskkill /IM chrome.exe')  # 关闭chrome
os.system('taskkill /IM chrome.exe')  # 关闭chrome
global driver
driver = webdriver.Chrome()  # 设置Chrome驱动


# ========================= 邮箱-无需修改 =========================
class Mail:
    def __init__(self):
        # 第三方 SMTP 服务

        self.mail_host = "smtp.qq.com"  # 设置服务器:这个是qq邮箱服务器，直接复制就可以
        self.mail_pass = "fdetqqwweyqerqg"  # 刚才我们获取的授权码
        self.sender = 'zolty@foxmail.com'  # 你的邮箱地址
        self.receivers = ['############']  # 收件人的邮箱地址，可设置为你的QQ邮箱或者其他邮箱，可多个

    def send(self, mail_content):
        content = mail_content
        message = MIMEText(content, 'plain', 'utf-8')

        message['From'] = 'zolty@vip.com'
        message['To'] = 'YourMajesty@vip.com'

        subject = str(time.localtime().tm_hour) +"点打卡"  # 发送的主题，可自由填写
        message['Subject'] = Header(subject, 'utf-8')
        try:
            smtpObj = smtplib.SMTP_SSL(self.mail_host, 465)
            smtpObj.login(self.sender, self.mail_pass)
            smtpObj.sendmail(self.sender, self.receivers, message.as_string())
            smtpObj.quit()
            print('邮件发送成功')
        except smtplib.SMTPException as e:
            print('邮件发送失败')


def web_init():
    driver.maximize_window()  # 最大化窗口
    driver.implicitly_wait(6)  # 隐式等待
    driver.get(login_url)  # 获取网页


def web_close():
    driver.quit()  # 关闭所有关联的窗口


def web_load():
    time.sleep(loading_time)  # 预留页面加载时间5秒


def auto_login():
    web_load()
    driver.find_element_by_xpath(uid_xpath).clear()  # 清空已存在内容
    driver.find_element_by_xpath(uid_xpath).send_keys(username)  # 传入用户名
    web_load()
    driver.find_element_by_xpath(pwd_xpath).clear()  # 清空已存在内容
    driver.find_element_by_xpath(pwd_xpath).send_keys(pwd)  # 传入密码
    web_load()
    driver.find_element_by_xpath(button_xpath).click()  # 通过登录按钮并单击登录


def submit():
    driver.get(submit_url)  # 获取网页
    web_load()
    driver.find_element_by_xpath(zy_xpath).clear()  # 清空“专业”中已存在内容
    web_load()
    driver.find_element_by_xpath(zy_xpath).send_keys(zy)  # 传入“专业”
    web_load()
    driver.find_element_by_xpath(nj_xpath).click()  # 点击年级
    web_load()
    driver.find_element_by_xpath(nj2020_xpath).click()  # 选择2020
    web_load()
    driver.find_element_by_xpath(xq_xpath).click()  # 点击校区
    web_load()
    driver.find_element_by_xpath(zxxq_xpath).click()  # 选择中心校区
    web_load()
    driver.find_element_by_xpath(sj_xpath).send_keys(sjh)  # 传入“手机号”
    web_load()
    driver.find_element_by_xpath(yjsms_xpath).send_keys(yjsms)  # 传入“研究生秘书”
    web_load()
    driver.find_element_by_xpath(yjsms_xpath).send_keys(Keys.ENTER)  # 回车
    web_load()
    #driver.find_element_by_xpath(gylmc_xpath).click()  # 点击公寓楼名称
    #web_load()
    #driver.find_element_by_xpath(nybgy_xpath).click()  # 选择南苑8公寓
    #web_load()
    #driver.find_element_by_xpath(qsh_xpath).clear()  # 清空“寝室号”中已存在内容
    #web_load()
    #driver.find_element_by_xpath(qsh_xpath).send_keys(qsh)  # 传入“寝室号”
    #web_load()
    driver.find_element_by_xpath(xw_xpath).click()  # 选择校外
    web_load()
    driver.find_element_by_xpath(dzse_xpath).send_keys(dzse)  # 传入“地址省”
    web_load()
    driver.find_element_by_xpath(dzse_xpath).send_keys(Keys.ENTER)  # 回车
    web_load()
    driver.find_element_by_xpath(dzsi_xpath).send_keys(dzsi)  # 传入“地址市”
    web_load()
    driver.find_element_by_xpath(dzsi_xpath).send_keys(Keys.ENTER)  # 回车
    web_load()
    driver.find_element_by_xpath(dzq_xpath).send_keys(dzq)  # 传入“地址区”
    web_load()
    driver.find_element_by_xpath(dzq_xpath).send_keys(Keys.ENTER)  # 回车
    web_load()
    driver.find_element_by_xpath(dzxx_xpath).send_keys(dzxx)  # 传入“地址详细”
    web_load()
    driver.find_element_by_xpath(dzxx_xpath).send_keys(Keys.ENTER)  # 回车
    web_load()
    driver.find_element_by_xpath(gfxf_xpath).click()  # 选择"高风险否"
    web_load()
    driver.find_element_by_xpath(bs_xpath).click()  # 选择博士
    web_load()
    if time.localtime().tm_hour < 12:
        driver.find_element_by_xpath(q1_xpath).click()
    web_load()
    driver.execute_script("window.scrollTo(0,document.body.scrollHeight);") # 将页面滚动条滑到底部
    web_load()
    driver.find_element_by_xpath(confirm_xpath).click()  # 确认填报
    web_load()
    driver.find_element_by_xpath(ok_xpath).click()  # 好
    web_load()


def remind():
    driver.get(remind_url)  # 获取网页
    web_load()
    row = driver.find_elements_by_tag_name('tr')
    result_list = []
    for i in row:
        j = i.find_elements_by_tag_name('td')
        for item in j:
            text = item.text
            result_list.append(text)
    today = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(time.time()))
    return today + '-' + result_list[0] + '-' + result_list[1] + '-' + result_list[2] + '-' + result_list[3]


if __name__ == '__main__':
    web_init()  # 驱动初始化
    auto_login()  # 登录操作
    submit()  # 打卡操作
    result_msg = remind()  # 获取提醒信息
    Mail().send(result_msg)
    web_close()  # 测试结束
